<?php 
namespace App\Models;

class ShortMsg extends BaseModel{
	
	//表明
	protected $table = 'short_msg';
	//主键
	protected $primaryKey = 'id';
    
    
	public function processPhone(){
        $phone = $this->request->input('phone');
        $shortMsg = $this->where('phone','=',$phone)
                ->first();
		if(!$shortMsg) {
			$shortMsgModel = new ShortMsg($this->request);
			$shortMsgModel->phone = $phone; 
            if($shortMsgModel->save()){
                $shortMsg = $shortMsgModel;
            }
		}
        return $shortMsg;
    }
    
    /* *
	 * @description :比较验证码
	 * @param:array
	 * @return: 0true 一样，false 不一样
	 */
	public function compareCode(){
        $phone = $this->request->input('phone');
        $verifyCode = $this->request->input('verifyCode');
		$shortMsg= ShortMsg::select('id','send_at','times','code')
			->where('phone', '=', $phone)
			->first();
		if(!$shortMsg){
			return false;
		}
		//1、判断是此时的验证码 是否有效（如 期限）
		if(time() - $shortMsg->send_at > 1800 || $shortMsg->times > 10){
			$shortMsg->code = 0 ; // 销毁验证码
            $shortMsg->times = 0;
			$shortMsg->save();
			return false; //验证码过期
		}
		//2、判断验证码是否一致
		if($shortMsg->code && $verifyCode == $shortMsg->code){
			$shortMsg->code = 0 ; // 销毁验证码
            $shortMsg->times = 0;
			$shortMsg->save();
			return TRUE;
		}  else {
			//验证码期限正确，但不一致
            $shortMsg->times = $shortMsg->times + 1;
			$shortMsg->save();
			return false;
		}
	}
}